package com.twu.biblioteca;

import java.util.ArrayList;

class MovieSection {

    ArrayList<Movie> allMovies = new ArrayList<Movie>();

    protected ArrayList<Movie> availableMovies(){
        ArrayList<Movie> testMovies = new ArrayList<Movie>();
        for (Movie movie:allMovies){
            if (movie.available)
                testMovies.add(movie);
        }
        return testMovies;
    }

    public int numberOfAvailableMovies(){
        return availableMovies().size();
    }

    protected void releaseMovie(String name){
        if (movieIsValidForRelease(name)){
            availableMovies().remove(selectMovie(name));
            (selectMovie(name)).available = false;
        }
    }

    protected void acceptMovie(String name){
        if (movieIsValidForReturn(name)){
            availableMovies().add(selectMovie(name));
            (selectMovie(name)).available = true;
        }
    }

    public void printAvailableMovies(){
        System.out.println("Available movies: ");
        for (Movie movie: availableMovies() ){
            System.out.println(movie.name);
        }
    }

    protected boolean movieIsValidForRelease(String name){
        for (Movie movie: availableMovies()){
            if (movie.name.equals(name))
                return true;
        }
        return false;
    }

    protected boolean movieIsValidForReturn(String name){
        for (Movie movie: allMovies){
            if (movie.name.equals(name) && !movie.available){
                return true;
            }
        }
        return false;
    }

    protected Movie selectMovie(String name){
        for (Movie movie :allMovies){
            if ((movie.name).equals(name))
                return movie;
        }
        return null;
    }

}
